package com.hanthink.system.service.impl.manhour;

import java.util.Date;
import java.util.List;

import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.EmRoaster;
import com.hanthink.system.domain.pc.EmEquip;
import com.hanthink.system.service.impl.SysUserServiceImpl;
import com.hanthink.system.service.pc.IEmEquipService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hanthink.system.mapper.manhour.MachineTheoryWorkersMapper;
import com.hanthink.system.domain.manhour.MachineTheoryWorkers;
import com.hanthink.system.service.manhour.IMachineTheoryWorkersService;
import com.hanthink.system.mapper.equipment.EquipmentInfoMapper;
import com.hanthink.system.domain.equipment.EquipmentInfo;
import com.hanthink.system.service.equipment.IEquipmentInfoService;
//import sun.tools.tree.IfStatement;

/**
 * 设备物料理论用工数Service业务层处理
 * 
 * @author hanthink
 * @date 2021-11-22
 */
@Service
public class MachineTheoryWorkersServiceImpl implements IMachineTheoryWorkersService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private MachineTheoryWorkersMapper machineTheoryWorkersMapper;

    @Autowired
    private IEmEquipService emEquipService;



    /**
     * 查询设备物料理论用工数
     * 
     * @param id 设备物料理论用工数ID
     * @return 设备物料理论用工数
     */
    @Override
    public MachineTheoryWorkers selectMachineTheoryWorkersById(Long id)
    {
        return machineTheoryWorkersMapper.selectMachineTheoryWorkersById(id);
    }

    /**
     * 查询设备物料理论用工数列表
     * 
     * @param machineTheoryWorkers 设备物料理论用工数
     * @return 设备物料理论用工数
     */
    @Override
    public List<MachineTheoryWorkers> selectMachineTheoryWorkersList(MachineTheoryWorkers machineTheoryWorkers)
    {
        return machineTheoryWorkersMapper.selectMachineTheoryWorkersList(machineTheoryWorkers);
    }

    /**
     * 新增设备物料理论用工数
     * 
     * @param machineTheoryWorkers 设备物料理论用工数
     * @return 结果
     */
    @Override
    public int insertMachineTheoryWorkers(MachineTheoryWorkers machineTheoryWorkers)
    {
        return machineTheoryWorkersMapper.insertMachineTheoryWorkers(machineTheoryWorkers);
    }

    /**
     * 修改设备物料理论用工数
     * 
     * @param machineTheoryWorkers 设备物料理论用工数
     * @return 结果
     */
    @Override
    public int updateMachineTheoryWorkers(MachineTheoryWorkers machineTheoryWorkers)
    {
        return machineTheoryWorkersMapper.updateMachineTheoryWorkers(machineTheoryWorkers);
    }


    @Override
    public int updateMachineTheoryWorkersMaterial(String MachineID,String MaterialID,String operationworks,String techworks)
    {
        return machineTheoryWorkersMapper.updateMachineTheoryWorkersMaterial(MachineID,MaterialID,operationworks,techworks);
    }
    /**
     * 批量删除设备物料理论用工数
     * 
     * @param ids 需要删除的设备物料理论用工数ID
     * @return 结果
     */
    @Override
    public int deleteMachineTheoryWorkersByIds(Long[] ids)
    {
        return machineTheoryWorkersMapper.deleteMachineTheoryWorkersByIds(ids);
    }

    /**
     * 删除设备物料理论用工数信息
     * 
     * @param id 设备物料理论用工数ID
     * @return 结果
     */
    @Override
    public int deleteMachineTheoryWorkersById(Long id)
    {
        return machineTheoryWorkersMapper.deleteMachineTheoryWorkersById(id);
    }

    @Override
    public String importMachineTheoryWorkersitem(List<MachineTheoryWorkers> MachineTheoryWorkersItemList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(MachineTheoryWorkersItemList) || MachineTheoryWorkersItemList.size() == 0) {
            throw new CustomException("导入的机台理论用工数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int currentRowNum = 0;
        for (MachineTheoryWorkers MachineTheoryWorkersItem : MachineTheoryWorkersItemList) {
            currentRowNum++;
            try {
                MachineTheoryWorkers items = machineTheoryWorkersMapper.selectMachineTheoryWorkersByMaterial(MachineTheoryWorkersItem.getMachineid(),MachineTheoryWorkersItem.getMaterialid());//.getPlanworkdatetime(),prdPlanItem.getPshiftid(),prdPlanItem.getMachineid() ,prdPlanItem.getMaterialid()) ;

                EmEquip emEquip =new EmEquip();
                emEquip.setSiteKey(MachineTheoryWorkersItem.getCompanyid());
                emEquip.setName(MachineTheoryWorkersItem.getMachineid());
                Integer itemses = emEquipService.selectEmEquipExist(emEquip);

                if (StringUtils.isNull(items)) {
                    java.util.Date day=new Date();
                    MachineTheoryWorkersItem.setCreator("admin");
                    MachineTheoryWorkersItem.setCreatedatetime(day);
                    //1、不存在于设备台账中的设备，不能导入；
                    //2、技术员人数、操作工人数，不能为空或0；
                    if (itemses==0){
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "设备编号：" + MachineTheoryWorkersItem.getMachineid()+ "设备名称："+MachineTheoryWorkersItem.getMachinename()+"不存在于设备台账中的设备，不能导入已存在");
                        continue;
                    }
                    if(MachineTheoryWorkersItem.getTechworks()==0 || MachineTheoryWorkersItem.getOperationworks()==0){
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "技术员人数、操作工人数，不能为空或0，不能导入");
                        continue;
                    }
                    this.insertMachineTheoryWorkers(MachineTheoryWorkersItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 导入成功");
                } else {
                    MachineTheoryWorkersItem.setUpdateBy(operName);

                    if (itemses == 0) {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "设备编号：" + MachineTheoryWorkersItem.getMachineid()+ "设备名称：" + MachineTheoryWorkersItem.getMachinename() + "不存在于设备台账中的设备，不能导入已存在");
                        continue;
                    }
                    if (MachineTheoryWorkersItem.getTechworks() == 0 || MachineTheoryWorkersItem.getOperationworks() == 0) {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "技术员人数、操作工人数，不能为空或0，不能导入");
                        continue;
                    }
                    this.updateMachineTheoryWorkersMaterial(MachineTheoryWorkersItem.getMachineid(),MachineTheoryWorkersItem.getMaterialid(),MachineTheoryWorkersItem.getOperationworks().toString(),MachineTheoryWorkersItem.getTechworks().toString());
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 更新成功");
                    //successMsg.append("<br/>" + successNum + "、生产班组 " + EmRoasterItem.getFactoryid() + EmRoasterItem.getCreatedatetime() +EmRoasterItem.getPlandatetime()+EmRoasterItem.getShiftid() +EmRoasterItem.getTeamid() + " 更新成功");
                    continue;
                }
//                } else {
//                    failureNum++;
//                    //failureMsg.append("<br/>" + failureNum + " 已存在");
//                    continue;
//                }
            }catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
